<script setup lang="ts">
import type { Recordable } from '@vben/types';

import type { VxeGridProps } from '#/adapter/vxe-table';

import { watch } from 'vue';

import { Page, useVbenDrawer } from '@vben/common-ui';
import { DictEnum } from '@vben/constants';
import { getPopupContainer } from '@vben/utils';

import { PlusOutlined } from '@ant-design/icons-vue';
import { Popconfirm, Space } from 'ant-design-vue';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { attributeList, removeAttribute } from '#/api/iot/product/attribute';
import { renderDict } from '#/utils/render';

import modelAttributeFormModal from './model-attribute-form-modal.vue';

interface Props {
  blockId: string;
  productId: string;
}

const props = defineProps<Props>();

watch(
  () => props.blockId,
  async (newProductId) => {
    if (newProductId) {
      await tableApi.query();
    }
  },
);

const gridOptions: VxeGridProps = {
  columns: [
    {
      title: '功能类型',
      field: 'functionType',
      slots: {
        default: ({ row }) => {
          return renderDict(`${row.functionType}`, DictEnum.IOT_FUNCTION_TYPE);
        },
      },
    },
    {
      title: '功能名称',
      field: 'name',
    },
    {
      title: '标识符',
      field: 'identifier',
    },
    {
      title: '数据类型',
      field: 'dataType',
    },
    {
      field: 'action',
      fixed: 'right',
      slots: { default: 'action' },
      title: '操作',
      resizable: false,
    },
  ],
  keepSource: true,
  minHeight: 300,
  pagerConfig: {
    total: 0,
    currentPage: 1,
    pageSize: 10,
  },

  proxyConfig: {
    autoLoad: false,
    ajax: {
      query: async ({ page }, formValues = {}) => {
        const data = await attributeList({
          blockId: props.blockId,
          productId: props.productId,
          pageNum: page.currentPage,
          pageSize: page.pageSize,
          ...formValues,
        });

        return { rows: data.rows, total: data.total };
      },
    },
  },
  toolbarConfig: {
    custom: false,
  },
  rowConfig: {
    isHover: true,
    keyField: 'id',
  },
};

const [BasicTable, tableApi] = useVbenVxeGrid({
  gridOptions,
});

const [ModelAttributeFormModal, drawerApi] = useVbenDrawer({
  connectedComponent: modelAttributeFormModal,
});

function handleAdd() {
  drawerApi.setData({
    blockId: props.blockId,
  });
  drawerApi.open();
}

async function handleEdit(record: Recordable<any>) {
  drawerApi.setData({ id: record.id });
  drawerApi.open();
}

async function handleDelete(row: Recordable<any>) {
  await removeAttribute(row.id);
  await tableApi.query();
}
</script>

<template>
  <Page :auto-content-height="false" style="margin-top: -25px">
    <BasicTable>
      <template #toolbar-actions>
        <a-button
          type="primary"
          v-access:code="['iot:property:add']"
          @click="handleAdd"
          :disabled="!props.blockId"
        >
          <PlusOutlined />
          {{ $t('pages.common.add') }}
        </a-button>
      </template>
      <template #action="{ row }">
        <Space>
          <ghost-button
            v-access:code="['iot:property:edit']"
            @click.stop="handleEdit(row)"
          >
            {{ $t('pages.common.edit') }}
          </ghost-button>
          <Popconfirm
            :get-popup-container="getPopupContainer"
            placement="left"
            title="确认删除？"
            @confirm="handleDelete(row)"
          >
            <ghost-button
              danger
              v-access:code="['iot:property:remove']"
              @click.stop=""
            >
              {{ $t('pages.common.delete') }}
            </ghost-button>
          </Popconfirm>
        </Space>
      </template>
    </BasicTable>
    <ModelAttributeFormModal @reload="tableApi.query()" />
  </Page>
</template>
